﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Web.Http;
using MVCManage.Api;
using MVCManage.Api.Models;
using MVCManage.BLL;
using MVCManage.Model;
namespace MVCManage.Api.Controllers
{
    public class GoodsController : ApiController
    {
        GoodsTypeBLL bll = new GoodsTypeBLL();
        GoodsBLL bb = new GoodsBLL();
        [Route("api/Goods/BuyTypeGood")]
        // GET: api/Goods
        public List<UniAppGoodJsonDto> GetGoods() {
            var list = from t in bll.Dal.Query()
                       select new UniAppGoodJsonDto
                       {
                           id = t.gtId,
                           name = t.name,
                           icon = t.icon,
                           is_show_backstage = t.is_show_backstage,
                           sort = t.sort,
                           goods_list = from g in t.Goods
                            select new UniAppGoodDto
                            {
                                id = g.gId,
                                name = g.name,
                                content = g.content,
                                images = g.images,
                                use_spec = g.use_spec,
                                use_property = g.use_property,
                                unit = g.unit,
                                price = g.price,
                                sales = g.sales,
                                sort = g.sort,
                               On_sole=g.on_sale,
                                property = from p in g.GoodsPropertyKey
                                select new UniAppGoodPropertyKeyDto
                                 {
                                id = p.gpkId,
                                name = p.GoodsSpecKey.name,
                                values = from v in p.GoodsPropertyValue
                                 select new UniAppGoodPropertyValueDto
                                  {
                                     id = v.gpvId,
                                     is_default = v.is_default,
                                     code = v.GoodsSpecValue.code,
                                     value = v.GoodsSpecValue.value
                                          }
                                       }

                              }
                       };
            return list.ToList();

        }

        [HttpGet]
        [Route("Api/Goods/Page")]
        public ResultPageList<GoodDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            List<Goods> data =bb.GetGoodsPageList(name, page, limit, out count);

            var list = from g in data
                           // 商品信息
                       select new GoodDto
                       {
                           GID = g.gId,
                           Name = g.name,
                           Content = g.content,
                           Images = g.images,
                           Sales = g.sales,
                           Unit = g.unit,
                           Use_Spec = g.use_spec,
                           Use_Property = g.use_property,
                           GTID = g.gtId,
                           TypeName = g.GoodsType.name,
                           Price = g.price,
                           On_sale=g.on_sale,
                           GoodPropertyKey = from gpk in g.GoodsPropertyKey
                                                 //商品的属性 温度 糖度 配料
                                             select new GoodPropertyKeyDto
                                             {
                                                 GPKID = gpk.gpkId,
                                                 Name = gpk.GoodsSpecKey.name,
                                                 GSKID = gpk.gskid,
                                                 GoodPropertyValue = from gpv in gpk.GoodsPropertyValue
                                                                         //属性值 
                                                                     select new GoodPropertyValueDto
                                                                     {
                                                                         GPVID = gpv.gpvId,
                                                                         GSVID = gpv.gsvId,
                                                                         Code = gpv.GoodsSpecValue.code,
                                                                         Value = gpv.GoodsSpecValue.value,
                                                                         Is_Default = gpv.is_default
                                                                     }
                                             }
                       };

            ResultPageList<GoodDto> result = new ResultPageList<GoodDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = list.ToList();

            return result;
        }

        // GET: api/Goods/5
    

        // POST: api/Goods
        public Result Post([FromBody] GoodEditDto value)
        {
            Goods good = new Goods();
            good.gId = 0;
            good.name = value.Name;
            good.price = value.Price;
            good.unit = value.Unit;
            good.gtId = value.GTID;
            good.content = value.Content;
            good.images = value.Images;
            good.use_spec = value.Use_Spec;
            good.use_property = value.Use_Property;
            good.sales =0;
            good.on_sale = true;
            good.sort = value.Sort;
            good.GoodsPropertyKey = new List<GoodsPropertyKey>() { };
            foreach (GoodEditPropertyKeyDto gpk in value.GoodsPropertyKey)
            {
                GoodsPropertyKey key = new GoodsPropertyKey()
                {
                    gpkId = 0,
                    gskid = gpk.GSKID,
                    GoodsPropertyValue = new List<GoodsPropertyValue>() { }
                };
                good.GoodsPropertyKey.Add(key);
                int i = 0;
                foreach (int gsvid in gpk.GoodsPropertyValue)
                {
                    key.GoodsPropertyValue.Add(new GoodsPropertyValue()
                    {
                        gpvId = 0,
                        gsvId = gsvid,
                        is_default=(i==0)
            
                    });
                    i++;

                }

            }

            bool pd = bb.Add(good);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "添加成功" : "添加失败";
            return result;

        }

        // PUT: api/Goods/5
        public Result Put( [FromBody]GoodEditDto value)
        {
            var good = new Goods();
            good.gId = value.GID;
            good.name = value.Name;
            good.price = value.Price;
            good.unit = value.Unit;
            good.gtId = value.GTID;
            good.use_property = value.Use_Property;
            good.use_spec = false;
            good.content = value.Content;
            good.sales = value.Sales;
            good.on_sale = value.On_sale;
            good.images = value.Images;
            good.sort = value.Sort;
            good.GoodsPropertyKey = new List<GoodsPropertyKey>() { };
            foreach (GoodEditPropertyKeyDto gpk in value.GoodsPropertyKey)
            {
                GoodsPropertyKey key = new GoodsPropertyKey()
                {
                    gpkId = 0,
                    gId=good.gId,
                    gskid=gpk.GSKID,
                    GoodsPropertyValue=new List<GoodsPropertyValue>()

                };
                good.GoodsPropertyKey.Add(key);
                int i = 0;
                foreach ( int gsvid in gpk.GoodsPropertyValue)
                {
                    key.GoodsPropertyValue.Add(new GoodsPropertyValue()
                    {
                        gpvId = 0,
                        gsvId = gsvid,
                        is_default=(i==0)
                    });
                    i++;



                }

            }
            bool pd = bb.UpdateGood(good);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "修改成功" : "修改失败";
            return result;


        }

        // DELETE: api/Goods/5
        public Result Delete(int id)
        {
            bool pd = bb.DeleteGood(id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "删除成功" : "删除失败";
            return result;

        }
        [Route("api/Goods/Dels")]
        public Result Deletes(int[] ids) {
            bool pd = bb.DeleteGoods(ids);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "删除成功" : "删除失败";
            return result;
        }
        [Route("api/Goods/Shangjia")]
        public Result PutShangjia([FromBody] IdDto gid)
        {
            bool pd = bb.ShangJia(gid.Id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "上架成功" : "上架失败";
            return result;
        }
        [Route("api/Goods/Xiajia")]
        public Result PutXiajia([FromBody] IdDto gid)
        {
            bool pd = bb.XiaJia(gid.Id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "下架成功" : "下架失败";
            return result;
        }
    }
}
